package com.jiang.myfm.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiang.myfm.R;
import com.jiang.myfm.entities.billboard.DataList;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


/**
 * Created by Administrator on 2016/12/5.
 */
public class BillboardItemAdapter extends RecyclerView.Adapter<BillboardItemAdapter.ViewHolder> implements View.OnClickListener{

    private Context context;
    private List<DataList> list;
    private onChildClickListener listener;
    private RecyclerView recyclerView;

    public void setListener(onChildClickListener listener) {
        this.listener = listener;
    }

    public BillboardItemAdapter(Context context) {
        super();
        this.context = context;
        list = new ArrayList<>();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    public void addAll(Collection<? extends DataList> collection){
        list.addAll(collection);
        notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_4_item,parent,false);
        view.setOnClickListener(this);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        DataList item = list.get(position);
        Picasso.with(context).load(item.getCoverPath()).into(holder.itemIv);
        holder.titleTv.setText(list.get(position).getTitle());
        holder.billboard_1Tv.setText("1 "+list.get(position).getFirstKResults().get(0).getTitle());
        holder.billboard_2Tv.setText("2 "+list.get(position).getFirstKResults().get(1).getTitle());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        private ImageView itemIv;
        private TextView titleTv;
        private TextView billboard_1Tv;
        private TextView billboard_2Tv;
        public ViewHolder(View itemView) {
            super(itemView);
            itemIv = (ImageView) itemView.findViewById(R.id.item_iv);
            titleTv = ((TextView) itemView.findViewById(R.id.item_title));
            billboard_1Tv = ((TextView) itemView.findViewById(R.id.billboard_1));
            billboard_2Tv = ((TextView) itemView.findViewById(R.id.billboard_2));
        }
    }

    @Override
    public void onClick(View view) {
        if (recyclerView!=null && listener!=null){
            int position = recyclerView.getChildAdapterPosition(view);
            String title = list.get(position).getTitle();
            String key = list.get(position).getKey();
            String contentType = list.get(position).getContentType();
            listener.onChildClick(position,title,key,contentType);
        }
    }

    public interface onChildClickListener{
        public void onChildClick(int position,String title,String key,String contentType);
    }
}
